package com.example.uberorganizer;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

import com.example.uberorganizer.UserInterfaceOne.EventDesc;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.CalendarContract;
import android.support.v4.app.FragmentActivity;

public class Utility {
	public static ArrayList<String> nameOfEvent = new ArrayList<String>();
	public static ArrayList<String> startDates = new ArrayList<String>();
	public static ArrayList<String> endDates = new ArrayList<String>();
	public static ArrayList<String> descriptions = new ArrayList<String>();
	public static ArrayList<String> result = new ArrayList<String>();
	public static ArrayList<EventDesc> events = new ArrayList<EventDesc>();
	
	// Odczytywanie wydarzen z kalendarza
	public static ArrayList<String> readCalendarEvent(Context context) {
		Cursor cursor = context.getContentResolver()
				.query(Uri.parse("content://com.android.calendar/events"),
						new String[] { "calendar_id", "title", "description",
								"dtstart", "dtend", "eventLocation" }, null,
						null, null);
		cursor.moveToFirst();
		// fetching calendars name
		String CNames[] = new String[cursor.getCount()];

		// fetching calendars id
		nameOfEvent.clear();
		startDates.clear();
		endDates.clear();
		descriptions.clear();
		events.clear();
		for (int i = 0; i < CNames.length; i++) {

			
			if((cursor.getString(4)!=null)&&(cursor.getString(3)!=null))
			{
			nameOfEvent.add(cursor.getString(1));
			startDates.add(getDate(Long.parseLong(cursor.getString(3))));
			endDates.add(getDate(Long.parseLong(cursor.getString(4))));
			descriptions.add(cursor.getString(2));
			CNames[i] = cursor.getString(1);
			
			Date d = new Date(Long.parseLong(cursor.getString(3)));
			result.add(d.toString() + " "+ cursor.getString(1) + " "+ cursor.getString(2));
			}
			cursor.moveToNext();

		}
		return result;
	}


	public static String getDate(long milliSeconds) {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		Calendar calendar = Calendar.getInstance();
		calendar.setTimeInMillis(milliSeconds);
		return formatter.format(calendar.getTime());
	}
	
	
	// wpisywanie zdarzenia do kalendarza
	public static void putCalendarEvent(Context context, String title, String description, long startTimeMillis, long endTimeMillis)
	{
		System.out.println(title + " " + description + " " + startTimeMillis + " " + endTimeMillis);
		ContentResolver cr = context.getContentResolver();
		   final ContentValues event = new ContentValues();

		    event.put(CalendarContract.Events.CALENDAR_ID, 1);

		    event.put(CalendarContract.Events.TITLE, title);
		    event.put(CalendarContract.Events.DESCRIPTION, description);

		    System.out.println(startTimeMillis);
		    event.put(CalendarContract.Events.DTSTART, startTimeMillis);
		    event.put(CalendarContract.Events.DTEND, (startTimeMillis + 86400*1000));
		    event.put(CalendarContract.Events.ALL_DAY, 1);   // 0 for false, 1 for true
		    event.put(CalendarContract.Events.HAS_ALARM, 1); // 0 for false, 1 for true

		    TimeZone timeZone = TimeZone.getDefault();
		    event.put(CalendarContract.Events.EVENT_TIMEZONE, timeZone.getID());

		    Uri uri = cr.insert(CalendarContract.Events.CONTENT_URI, event);
	}
}
